package com.imooc.servlet.cookie;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/cookies/index")
public class ImoocIndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//request.getCookies()方法获取所有的cookie信息
		Cookie[] cs = request.getCookies();
		//根据cookie里面是否有数据
		//判断用户是否登录，登录后显示用户名，没有登录显示user not login
		String user = null;
		for (Cookie c : cs) {
			System.out.println(c.getName() + ":" +c.getValue());
			if(c.getName().equals("user")) {
				user = c.getValue();
				break;
			}
		}
		if(user == null) {
			response.getWriter().println("user not login");
		}else {
			response.getWriter().println("user:" + user);

		}
	}



}
